//
//  AchieveViewController.m
//  沙盒路径
//
//  Created by yuanjiang on 2016/12/6.
//  Copyright © 2016年 YuanJiang. All rights reserved.
//

#import "AchieveViewController.h"
#import "Person.h"
@interface AchieveViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tf1;
@property (weak, nonatomic) IBOutlet UITextField *tf2;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UITextField *tf3;
@property (weak, nonatomic) IBOutlet UITextField *tf4;
@property (weak, nonatomic) IBOutlet UITextField *tf5;
@property (weak, nonatomic) IBOutlet UITextField *tf6;

@end

@implementation AchieveViewController

//归档
- (IBAction)AchieveAction:(id)sender {
    //1.将自定义类的对象,转化为NSData对象
    Person *per = [[Person alloc] init];
    Person *per1= [[Person alloc] init];
    per.name = self.tf1.text;//姓名为第一个输入框内的文字
    per.gender = self.tf2.text;
    per.age = [self.tf3.text integerValue];
    
    per1.name = self.tf4.text;
    per1.gender = self.tf5.text;
    per1.age = [self.tf6.text  integerValue];
    NSArray *arr = @[per,per1];
    
    //将per对象转化为NSMutableData 对象
    NSMutableData *data = [NSMutableData data];
    //创建归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //归档操作
    [archiver encodeObject:arr forKey:@"ssss"];
    //结束归档
    [archiver finishEncoding];
    //获取文件路径
    NSString *documentFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //拼接路径
    NSString *perFilePath =[documentFilePath stringByAppendingPathComponent:@"per.archiver"];
    //3.写入文件当中
    BOOL isSuccess = [data writeToFile:perFilePath atomically:YES];
    NSLog(@"%@",isSuccess?@"写入成功":@"写入失败");
}
//犯归档
- (IBAction)UnAchieveAction:(id)sender {
    //1.获取Decuments路径
    NSString *documentFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    //2.拼接路径
    NSString *perFilePath = [documentFilePath stringByAppendingPathComponent:@"per.archiver"];
    NSData *data = [NSData dataWithContentsOfFile:perFilePath];
    //3.创建反归档工具
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    //4.反归档
    NSArray *arr = [unarchiver decodeObjectForKey:@"ssss"];
    //5.结束反归档
    [unarchiver finishDecoding];
    self.tf1.text = ((Person *)arr[0]).name;
    self.tf2.text = ((Person *)arr[0]).gender;
    self.tf3.text = [NSString stringWithFormat:@"%ld",((Person *)arr[0]).age];
    
    self.tf4.text = ((Person *)arr[1]).name;
    self.tf5.text = ((Person *)arr[1]).gender;
    self.tf6.text = [NSString stringWithFormat:@"%ld",((Person *)arr[1]).age];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //对数组进行归档
    Person *per1 = [[Person alloc] init];
    Person *per2 = [[Person alloc] init];
    Person *per3 = [[Person alloc] init];
    //次处可以一一对对象里的属性name gender age 赋值
    NSArray *arr = @[per1,per2,per3];
    //1.创建归档工具
    NSMutableData *mData1 = [NSMutableData data];
    NSKeyedArchiver *archiver1 = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData1];
    //2.归档
    [archiver1 encodeObject:arr forKey:@"arr"];
    //3.结束归档
    [archiver1 finishEncoding];
    //当对数组进行归档时,会将数组中的每个元素进行归档,所以Person类需要服从NSCoding协议,实现方法.
    //写入文件
    [mData1 writeToFile:[self filePath] atomically:YES];
    
    //对数组进行反归当
    NSKeyedUnarchiver *unarchiver1 = [[NSKeyedUnarchiver alloc] initForReadingWithData:mData1];
    //反归档得到的还是一个数组,数组中存放的依然是Person 对象
    NSArray *arr1 =[unarchiver1 decodeObjectForKey:@"arr"];
    [unarchiver1 finishDecoding];
    //反归档之后，测试有没有取到对象
    NSLog(@"arr1 = %@",arr1);
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//获取文件路径
-(NSString *)filePath
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    return [documentPath stringByAppendingPathComponent:@"per.data"];//?
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
